#include <iostream>
using namespace std;

/*
    【题目245】编写一个程序，重载下标运算符0，使其具有动态检测下标地址是否越界的功能。
*/

template <typename T>
class Aipoint
{
public:
    Aipoint(int len)
    {
        this->len = len;
        this->p = new T[len];
    }

    ~Aipoint()
    {
        delete[] p;
    }

    T &operator[](int index)
    {
        if (index < 0 || index >= len)
        {
            throw std::out_of_range("index error");
        }
        return this->p[index];
    }

public:
private:
    T *p;
    int len;
};

int main()
{
    Aipoint<int> a(10);
    a[0] = 1;
    a[1] = 2;
    a[2] = 3;
    a[3] = 4;
    a[4] = 5;
    a[5] = 6;
    a[6] = 7;

    cout << a[0] << endl;
    cout << a[5] << endl;
}